package com.bank.service.impl;

import com.bank.entity.TradeInfo;
import com.bank.mapper.TradeInfoMapper;
import com.bank.mapper.CardInfoMapper;
import com.bank.service.TradeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 交易信息服务实现类
 */
@Service
public class TradeInfoServiceImpl implements TradeInfoService {

    @Autowired
    private TradeInfoMapper tradeInfoMapper;

    @Autowired
    private CardInfoMapper cardInfoMapper;

    @Override
    public List<TradeInfo> findAll() {
        return tradeInfoMapper.findAll();
    }

    @Override
    public List<TradeInfo> findByCardId(String cardID) {
        return tradeInfoMapper.findByCardId(cardID);
    }

    @Override
    @Transactional
    public boolean add(TradeInfo tradeInfo) {
        return tradeInfoMapper.insert(tradeInfo) > 0;
    }

    @Override
    @Transactional
    public boolean delete(String cardID, String tradeDate) {
        return tradeInfoMapper.delete(cardID, tradeDate) > 0;
    }

    @Override
    @Transactional
    public boolean deposit(TradeInfo tradeInfo) {
        int tradeResult = tradeInfoMapper.insert(tradeInfo);
        int cardResult = cardInfoMapper.increaseBalance(tradeInfo.getCardID(), tradeInfo.getTradeMoney());
        return tradeResult > 0 && cardResult > 0;
    }

    @Override
    @Transactional
    public boolean withdraw(TradeInfo tradeInfo) {
        int cardResult = cardInfoMapper.decreaseBalance(tradeInfo.getCardID(), tradeInfo.getTradeMoney());
        if (cardResult > 0) {
            int tradeResult = tradeInfoMapper.insert(tradeInfo);
            return tradeResult > 0;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean transfer(String sourceCardID, String targetCardID, java.math.BigDecimal amount) {
        // 1. 校验
        if (amount.compareTo(java.math.BigDecimal.ZERO) <= 0) return false;
        com.bank.entity.CardInfo source = cardInfoMapper.findById(sourceCardID);
        com.bank.entity.CardInfo target = cardInfoMapper.findById(targetCardID);
        if (source == null || target == null) return false;
        if (source.getBalance().compareTo(amount) < 0) return false;
        // 2. 插入交易记录
        com.bank.entity.TradeInfo outTrade = new com.bank.entity.TradeInfo();
        outTrade.setCardID(sourceCardID);
        outTrade.setTradeType("支取");
        outTrade.setTradeMoney(amount);
        outTrade.setTradeDate(java.time.LocalDateTime.now());
        outTrade.setRemark("转账到 " + targetCardID);
        tradeInfoMapper.insert(outTrade);
        com.bank.entity.TradeInfo inTrade = new com.bank.entity.TradeInfo();
        inTrade.setCardID(targetCardID);
        inTrade.setTradeType("存入");
        inTrade.setTradeMoney(amount);
        inTrade.setTradeDate(java.time.LocalDateTime.now());
        inTrade.setRemark("来自 " + sourceCardID);
        tradeInfoMapper.insert(inTrade);
        // 3. 修改余额
        int dec = cardInfoMapper.decreaseBalance(sourceCardID, amount);
        int inc = cardInfoMapper.increaseBalance(targetCardID, amount);
        return dec > 0 && inc > 0;
    }
}